<?php

class BaldaGrid extends ConnectionManager {

    var $cnx;
    var $selectValues;
    var $limit;
    var $pagina;
    var $ordenadoPor;
    var $orden;
    var $filtro;
    var $query;
    var $cantidad;
    var $resultado;
    var $cantidadQuery;

    public function getCnx() {
        return $this->cnx;
    }

    public function getSelectValues($value) {
        return $this->selectValues[$value];
    }

    public function setSelectValues($value) {
        $this->selectValues[$value] = $value;
    }

    public function getLimit() {
        return $this->limit;
    }

    public function setLimit($limit) {
        $this->limit = $limit;
    }

    public function getPagina() {
        return $this->pagina;
    }

    public function setPagina($pagina) {
        $this->pagina = $pagina;
    }

    public function getOrdenadoPor() {
        return $this->ordenadoPor;
    }

    public function setOrdenadoPor($ordenadoPor) {
        $this->ordenadoPor = $ordenadoPor;
    }

    public function getOrden() {
        return $this->orden;
    }

    public function setOrden($orden) {
        $this->orden = $orden;
    }

    public function getFiltro() {
        return $this->filtro;
    }

    public function setFiltro($filtro) {
        $this->filtro = $filtro;
    }

    public function getCantidad() {
        return $this->cantidad;
    }

    public function setCantidad($cantidad) {
        $this->cantidad = $cantidad;
    }

    public function getResultado() {
        return $this->resultado;
    }

    public function setResultado($resultado) {
        $this->resultado = $resultado;
    }

    public function getQuery() {
        return $this->query;
    }

    public function setQuery($query) {
        $this->query->append($query);
    }

    public function getCantidadQuery() {
        return $this->cantidadQuery;
    }

    public function setCantidadQuery($value) {
        $this->cantidadQuery->append($value);
    }

    function __construct() {
        $this->cnx = new ConnectionManager();
        $this->query = new ArrayObject();
        $this->selectValues = Array();
        $this->cantidadQuery = new ArrayObject();
    }

    public function get($values) {
        $this->setQuery("SELECT " . $values);
        foreach (explode(',', $values) as $value) {
            $this->setSelectValues($value);
        }
        return $this;
    }

    public function from($tableName) {
        $this->setQuery(" FROM " . $tableName);
        $this->setCantidadQuery("SELECT COUNT(id) as cantidad FROM " . $tableName);
        return $this;
    }

    public function filterBy($key, $value) {
        if ($value) {
            $this->setQuery(" WHERE " . $key . " LIKE '%" . $value . "%'");
            $this->setCantidadQuery(" WHERE " . $key . " LIKE '%" . $value . "%'");
        }
        return $this;
    }

    public function limit($limit, $pagina) {
        if ($limit) {
            $this->setQuery(" LIMIT " . $limit . " OFFSET " . ($limit * ($pagina - 1)));
        }
        return $this;
    }

    public function orderBy($orderBy, $order) {
        if ($orderBy) {
            $this->setQuery(" ORDER BY " . $orderBy . " " . $order);
        }
        return $this;
    }

    public function executeCantidadResultados() {
        foreach ($this->getCantidadQuery() as $value) {
            $q[] = $value;
        }
        $rlt = mysql_fetch_object(mysql_query(join(' ', $q), $this->getCnx()->getConnection())) or die("error en la consulta: " . join(' ', $q));
        $this->setCantidad($rlt->cantidad);
        unset($q);
        return $this;
    }

    private function execute() {
        foreach ($this->getQuery() as $queryValue) {
            $q[] = $queryValue;
        }
        $rlt = mysql_query(join(' ', $q), $this->getCnx()->getConnection()) or die("error en la consulta: " . join(' ', $q));
        unset($q);
        $this->executeCantidadResultados();
        $this->setResultado($rlt);
        return $this;
    }

    public function getSqlQuery() {
        foreach ($this->getQuery() as $queryValue) {
            echo $queryValue;
        }

        return $this;
    }

    public function getJsonResponse() {
        $resultados = $this->execute();
        $x = 0;
        $data[0] = $resultados->getCantidad();
        while ($row = mysql_fetch_array($resultados->getResultado(), MYSQL_ASSOC)) {
            $data[++$x] = $row;
        }

        header("Content-type: text/plain");
        echo json_encode($data);
        unset($data);
        return $this;
    }

}

?>